%Invoke mtest's runtests() for all of the Snow Dots directories
%
%   didPass = runAllDotsTests(varargin)
%
%   @param varargin optional parameter-value pairs for setting shared
%   property values, which will be applied before each test.
%   @details
%   @a varargin may also contain other parameters-value pairs which are not
%   normal shared object properties, but conveniences for testing.
%   Recognized conveniences are:
%       - 'lookMode', value should be a duration in seconds.  Unit tests
%       that draw graphics to the screen may wait for the duration of
%       lookMode to allow for visual inspection by a user.
%       .
%   @details
%   Run all unit test for the Snow Dots code.  Returns true if all
%   tests pass.  If any test fails, aborts and returns false.
%   @ingroup utilities

function didPass = runAllDotsTests(varargin)

clear global CONVENIENCE_MODE_STRUCT SHARED_PROP_STRUCT
global CONVENIENCE_MODE_STRUCT SHARED_PROP_STRUCT

if nargin
    try
        [vals, sharedProps] = ...
            dotsTheMachineConfiguration.getSettingsForClass('sharedDefaults');
        
        CONVENIENCE_MODE_STRUCT = struct;
        SHARED_PROP_STRUCT = struct;
        
        for ii = 1:2:nargin
            param = varargin{ii};
            val = varargin{ii+1};
            if ischar(param)
                if any(strcmp(param, sysModes))
                    SHARED_PROP_STRUCT.(param) = val;
                else
                    CONVENIENCE_MODE_STRUCT.(param) = val;
                end
            end
        end
        
    catch e
        warning('SNOW DOTS TESTS COULD NOT GET DEFAULT SHARED PROPERTIES FROM THE MACHINE CONFIGURATION')
        clear global CONVENIENCE_MODE_STRUCT SHARED_PROP_STRUCT
    end
end

% locate the dots code tree
[p, f] = fileparts(mfilename('fullpath'));
dotsRoot = fullfile(p, '..');

initialDir = pwd;

disp(sprintf('RUNNING ALL TESTS FOR SNOW DOTS'));
tic;
didPass = runTestsInDir(dotsRoot);
cd(initialDir)

if didPass
    disp(sprintf('\nSNOW DOTS PASSED ALL TESTS in %f seconds', toc));
end

function didPass = runTestsInDir(d)
cd(d);
absPath = pwd;
directoryList = dir(absPath);

disp(sprintf('\n\n%s', absPath));
didPass = runtests(absPath);

if didPass
    for ii = 1:length(directoryList)
        if directoryList(ii).isdir && isempty(regexp(directoryList(ii).name, '^\.'))
            % recursive: into subdirectories (ignore ".svn", etc)
            fullPath = fullfile(absPath, directoryList(ii).name);
            didPass = runTestsInDir(fullPath);
            if ~didPass
                break
            end
        end
    end
end